package com.castle.fortress.admin.cms.rest;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.castle.fortress.admin.cms.dto.CmsChannelDto;
import com.castle.fortress.admin.cms.entity.CmsChannelEntity;
import com.castle.fortress.admin.cms.service.CmsChannelService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * cms栏目/导航菜单配置表 api 控制器
 *
 * @author castle
 * @since 2022-03-09
 */
@Api(tags="cms栏目/导航菜单配置表api管理控制器")
@RestController
@RequestMapping("/api/cms/cmsChannel")
public class ApiCmsChannelController {
    @Autowired
    private CmsChannelService cmsChannelService;


    /**
     * cms栏目/导航菜单配置表的树形展示
     * @param cmsChannelDto cms栏目/导航菜单配置表实体类
     * @return
     */
    @ApiOperation("cms栏目/导航菜单配置表树形展示")
    @GetMapping("/tree")
    public RespBody<List<CmsChannelDto>> treeCmsChannel(CmsChannelDto cmsChannelDto){
	    List<CmsChannelDto> list = cmsChannelService.listCmsChannel(cmsChannelDto);
	    //展示时需要展示到顶级
	    Set<Long> ids=new HashSet<>();
	    for(CmsChannelDto s:list){
		    ids.add(s.getId());
	    }
	    List<Long> parentId =new ArrayList<>();
	    List<CmsChannelDto> parentList=new ArrayList<>();
	    for(CmsChannelDto s:list){
		    if(s.getParentId()!=null&&!ids.contains(s.getParentId())){
			    parentId.add(s.getParentId());
		    }
	    }
	    if(!parentId.isEmpty()){
		    QueryWrapper<CmsChannelEntity> wrapper=new QueryWrapper<>();
		    wrapper.in("id",parentId);
		    parentList= ConvertUtil.transformObjList(cmsChannelService.list(wrapper),CmsChannelDto.class);
		    list.addAll(parentList);
	    }
	    do{
		    parentId =new ArrayList<>();
		    for(CmsChannelDto s:parentList){
			    if(s.getParentId()!=null){
				    parentId.add(s.getParentId());
			    }
		    }
		    if(!parentId.isEmpty()){
			    QueryWrapper<CmsChannelEntity> wrapper=new QueryWrapper<>();
			    wrapper.in("id",parentId);
			    parentList= ConvertUtil.transformObjList(cmsChannelService.list(wrapper),CmsChannelDto.class);
			    list.addAll(parentList);
		    }
	    }while(!parentId.isEmpty());
	    return RespBody.data(ConvertUtil.listToTree(list));
    }

    /**
     * cms栏目/导航菜单配置表保存
     * @param cmsChannelDto cms栏目/导航菜单配置表实体类
     * @return
     */
    @ApiOperation("cms栏目/导航菜单配置表保存")
    @PostMapping("/save")
    public RespBody<String> saveCmsChannel(@RequestBody CmsChannelDto cmsChannelDto){
	    if(cmsChannelDto == null ){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
        CmsChannelEntity cmsChannelEntity = ConvertUtil.transformObj(cmsChannelDto,CmsChannelEntity.class);
	    if(cmsChannelEntity.getParentId()!=null){
              CmsChannelEntity parent = cmsChannelService.getById(cmsChannelEntity.getParentId());
		    if(StrUtil.isEmpty(parent.getParents())){
                cmsChannelEntity.setParents(parent.getId()+"");
		    }else{
                cmsChannelEntity.setParents(parent.getParents()+","+parent.getId());
		    }
	    }
	    if(cmsChannelService.save(cmsChannelEntity)){
		    return RespBody.data("保存成功");
	    }else{
		    return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
	    }
    }

    /**
     * cms栏目/导航菜单配置表编辑
     * @param cmsChannelDto cms栏目/导航菜单配置表实体类
     * @return
     */
    @ApiOperation("cms栏目/导航菜单配置表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateCmsChannel(@RequestBody CmsChannelDto cmsChannelDto){
	    if(cmsChannelDto == null || cmsChannelDto.getId() == null || cmsChannelDto.getId().equals(0L)){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
            CmsChannelEntity cmsChannelEntity = ConvertUtil.transformObj(cmsChannelDto,CmsChannelEntity.class);
	    if(cmsChannelEntity.getParentId()!=null){
              CmsChannelEntity parent = cmsChannelService.getById(cmsChannelEntity.getParentId());
		    if(StrUtil.isEmpty(parent.getParents())){
                cmsChannelEntity.setParents(parent.getId()+"");
		    }else{
                cmsChannelEntity.setParents(parent.getParents()+","+parent.getId());
		    }
	    }
	    if(cmsChannelService.updateById(cmsChannelEntity)){
		    return RespBody.data("保存成功");
	    }else{
		    return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
	    }
    }

    /**
     * cms栏目/导航菜单配置表删除
     * @param ids cms栏目/导航菜单配置表id集合
     * @return
     */
    @ApiOperation("cms栏目/导航菜单配置表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteCmsChannel(@RequestBody List<Long> ids){
	    if(ids == null || ids.size()<1){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
	    QueryWrapper<CmsChannelEntity> wrapper=new QueryWrapper<>();
	    wrapper.in("id",ids);
	    for(Long d:ids){
		    wrapper.or().like("parents",d);
	    }
	    if(cmsChannelService.remove(wrapper)) {
		    return RespBody.data("删除成功");
	    }else{
		    return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
	    }
    }

    /**
     * cms栏目/导航菜单配置表详情
     * @param id cms栏目/导航菜单配置表id
     * @return
     */
    @ApiOperation("cms栏目/导航菜单配置表详情")
    @GetMapping("/info")
    public RespBody<CmsChannelDto> infoCmsChannel(@RequestParam Long id){
	    if(id == null){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
                CmsChannelEntity cmsChannelEntity = cmsChannelService.getById(id);
                CmsChannelDto cmsChannelDto = ConvertUtil.transformObj(cmsChannelEntity,CmsChannelDto.class);

	    return RespBody.data(cmsChannelDto);
    }


}
